<?php
//if (!defined("ACCESS"))
//    die("Access denied");

$db = new myDBS();

//$sql = "select * from tbl_promise_budget";

$from = $_REQUEST["from"];
$to = $_REQUEST["to"];

if ($from && $to) {
    $where = " AND promise_date BETWEEN '$from' AND '$to'";
}

$sql = "SELECT tbl_promise_budget. * , panngan3_name, panngan2_name, expenses3_name, expenses2_name, pet_id "
        . " FROM tbl_promise_budget, panngan2, panngan3, expenses_lvl3, expenses_lvl2, tbl_pet_budget "
        . " WHERE tbl_pet_budget.promise_id = tbl_promise_budget.id "
        . " AND panngan2.panngan2_id = tbl_promise_budget.panngan2_id "
        . " AND panngan3.panngan3_id = tbl_promise_budget.panngan3_id "
        . " AND expenses_lvl3.expenses3_id = ex3_id "
        . " AND expenses_lvl2.expenses2_id = ex2_id "
        . " $where ";


$result = $db->runSQL($sql);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#example").dataTable();
        
        $("#from").datepicker({
            dateFormat: "yy-mm-dd",
            //showButtonPanel: true,
            defaultDate: "+1w",
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 1,
            onClose: function(selectedDate) {
                $("#to").datepicker("option", "minDate", selectedDate);
            }
        });
        $("#to").datepicker({
            dateFormat: "yy-mm-dd",
            //showButtonPanel: true,
            defaultDate: "+1w",
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 1,
            onClose: function(selectedDate) {
                $("#from").datepicker("option", "maxDate", selectedDate);
            }
        });
    
    });

</script>

<form action="" method="post" target="_blank">

    <div class="form-inline">
        <label for="from">จาก</label>
        <input type="text" id="from" name="from" required value ="<?php echo $from; ?>" />
        <label for="to">ถึง</label>
        <input type="text" id="to" name="to" required value ="<?php echo $to; ?>"/>
        <button type="submit" id="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> ดูรายงาน</button>
    </div>


</form>

<table id="example" class="table table-bordered">
    <caption><br></caption>
    <thead>
        <tr>
            <th>เลขที่สัญญา</th>
            <th>ชื่อ นามสกุล ผู้ยืม</th>
            <th>วันที่</th>
            <th>เลขที่ฎีกา</th>
            <th>หมวด</th>
            <th>ประเภทรายการ</th>
            <th>จำนวนเงิน</th>

        </tr>
    </thead>

    <tbody >
        <?php
        while ($rows = $db->fetch_object($result)) {
            ?>
            <tr class="<?php echo ($rows->status == "cancel") ? "error" : ""; ?> ">

                <td><?php echo $rows->id; ?></td>
                <td>
                    <?php echo get_name_person($rows->card_id1); ?>
                    <?php echo ($rows->card_id2) ? ", " . get_name_person($rows->card_id2) : ""; ?>
                    <?php echo ($rows->card_id3) ? " และ" . get_name_person($rows->card_id3) : ""; ?>
                </td>

                <td><?php echo $rows->promise_date; ?></td>
                <td><?php echo $rows->pet_id; ?></td>
                <td><?php echo $rows->expenses2_name; ?></td>
                <td><?php echo $rows->expenses3_name; ?></td>
                <td><p class="text-right"><?php echo number_format($rows->total, 2); ?></p></td>

            </tr>
            <?php
        }
        ?>
    </tbody>
</table>